package processor;

import javax.jms.JMSException;

import saver.SaveToDB;
import fetcher.DownloadFromMQ;

public class Main {
	private DownloadFromMQ downloadFromMQ;
	private SaveToDB saveToDB;
	
	public Main() throws JMSException{
		downloadFromMQ = new DownloadFromMQ();
		saveToDB = new SaveToDB();
	}

	/**
	 * receive the msg from MQ in ten times, once for 1000 msg
	 */
	public void start(){
		for(int i=0; i<10; i++){
			String[] strings = new String[1000];
			strings = downloadFromMQ.receive();
			boolean flag = saveToDB.save(strings);
			if(flag){
				try {
					downloadFromMQ.commit();
				} catch (JMSException e) {
					e.printStackTrace();
					break;
				}
			}else{
				break;
			}
		}
		downloadFromMQ.close();
	}
	
	public static void main(String[] args) throws JMSException{
		new Main().start();
	}

}
